package game.events 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author gcxk721
	 */
	public class GameDispatcher 
	{
		
		private static var instance : GameDispatcher;
		
		private var eventDispatcher:IEventDispatcher;
		
        private var _countDic:Dictionary;
		
		public function GameDispatcher(target : IEventDispatcher = null) 
		{
			this.eventDispatcher = new EventDispatcher(target);
            this._countDic = new Dictionary();
		}
		
		public function dispatchEvent(evt : Event):void
		{
            //var _local2:int = getTimer();
            this.eventDispatcher.dispatchEvent(evt);
            //if ((getTimer() - _local2) > 5){
            //    debug(("@" + _arg1.type), "消耗", (getTimer() - _local2), "-", getTimer());
            //}
        }
		
        public function hasEventListener(type : String):Boolean
		{
            return eventDispatcher.hasEventListener(type);
        }
		
        public function willTrigger(type:String):Boolean
		{
            return this.eventDispatcher.willTrigger(type);
        }
		
        public function getCountInfo(value : int = 0):String
		{
            var type : String;
            var result : String = "";
            for (type in this._countDic) 
			{
                if (this._countDic[type] > value)
				{
					result += type + "_" + this._countDic[type] + "\n";
                }
            }
            return result;
        }
		
        public function removeEventListener(type : String, listener : Function, useCapture : Boolean = false):void
		{
            this.eventDispatcher.removeEventListener(type, listener, useCapture);
			_countDic[type]--;
        }
		
        public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
            this.eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
			if (_countDic[type] == undefined)
			{
				_countDic[type] = 1;
			}
			else
			{
				_countDic[type]++;
			}
        }
		
		public static function getInstance() : GameDispatcher
		{
			if (instance == null)
			{
				instance = new GameDispatcher();
			}
			return instance;
		}
		
	}

}